package com.sergio.compress.cp.base;

import lombok.Data;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.zip.DataFormatException;

@Data
public abstract class CompressTest {


    private CompressResult compressResult;

    private DecompressResult decompressResult;


    public abstract String getType();

    public abstract CompressResult compress(TestSource testSource) throws IOException;

    public abstract DecompressResult decompress(CompressResult testSource) throws IOException, DataFormatException;


    public CompressTest test(String filePath) throws IOException, DataFormatException {
        File file = new File(filePath);
        if (file.exists()) {
            byte[] sourceData = Files.readAllBytes(file.toPath());
            return test(sourceData);
        } else {
            throw new RuntimeException("文件不存在 : " + filePath);
        }
    }

    public CompressTest test(byte[] source) throws IOException, DataFormatException {
        TestSource testSource = new TestSource(source);
        CompressResult compressResult = compress(testSource);
//        System.out.println(compressResult);
        DecompressResult decompressResult = decompress(compressResult);
        decompressResult.setResult(Arrays.equals(source, decompressResult.data));
//        System.out.println(decompressResult);
        this.compressResult = compressResult;
        this.decompressResult = decompressResult;
        return this;
    }

}
